package JCL;
# scan_log_for_jobno_and_userid($logs_ref) return \%jobno_userid.
# get_file_list($dir) $dir= '/home/ftpuser/' return the files array ref.
# get_date_list($dir) $dir= '/home/ftpuser/' return the last 5 days of date array ref.
# get_job_list($dir, $file) $dir= '/home/ftpuser/' return the jobs array ref.
# get_released_releasing($dir, $file) $dir= '/home/ftpuser/' return the job released & releasing hash ref.
# log_match($job_name, \@logs)  return \@found_log
# date_time($keyword) $keyword = 'today' || 'now' || 'today' || 'julian' || 'yesterday'
# date_from_yday($year_$yday) transfer yday to readl date. return 2010-07-25
# file_jobs_get_result($file, \@jobs, \@logs, $jobno_userid_ref, $released_ref, $releasing_ref) return \@jcl_jobs_detail

sub file_jobs_get_result {  #get array for display

#  my ($file_jobs_ref, $logs_ref, $jobno_userid_ref, $released_ref, $releasing_ref) = @_;
  my ($key, $value, $logs_ref, $jobno_userid_ref, $released_ref, $releasing_ref) = @_;

  my %found_log;
 
  my @jcl_job_step;
 #my @job_step;
  my $save_jcl=' ';
  my $save_job=' ';
  my $save_jobno=' ';
#  while (my ($key, $value) = each %$file_jobs_ref ) { #for every jobs of file
 #  print "$key=>\n";    # print File
    my $jcl = $key;
    foreach (@$value) {  # every Job
 #    print "$_==>\n";   # print job
      my $job = $_;
      $found_log{$_}=&JCL::log_match($_, $logs_ref); # parsing log for job then return the array_ref for steps
      my $job_ref = $found_log{$_};
      my $found_count = @$job_ref;
#    print "found GB22OK03! found_log count= $found_count\n" if $job eq 'GB22OK03';
      if ($found_count == 0) {
        my @job_step = ($jcl, $job, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
        push @jcl_job_step, \@job_step;
      }
      else {
        foreach my $step_ref (@$job_ref) {  # for every steps details
          my @job_step;
          my $date = &JCL::date_from_yday($$step_ref[0]);  # sub of yday => real date
          my $userid = $$jobno_userid_ref{$$step_ref[2]};
 
          if ($jcl eq $save_jcl) {
            $job_step[0] = ' ';
          }
          else {
            $job_step[0] = $jcl;       # jcl file name
            $save_jcl = $jcl;
          }
 
          if ($job eq $save_job) {
            $job_step[1] = ' ';
            $job_step[2] = ' ';
          }
          else {
            $job_step[1] = $$released_ref{$job}; # released job
            $job_step[1] = $job_step[1] . '=>' if $job_step[1]; # add '=>' if not space
            $job_step[2] = $job;                 # job name
            $job_step[8] = $$releasing_ref{$job}; # releasing job
            $job_step[8] = '=>' . $job_step[8] if $job_step[8]; # add '=>' if not space
            $save_job = $job;
          }
 
          $job_step[3] = $date;         # date
 
          $job_step[4] = $$step_ref[1]; # time
 
          if ($$step_ref[2] eq $save_jobno) {
            $job_step[5] = ' ';
          }
          else {
            $job_step[5] = $$step_ref[2]; # job No.
            $save_jobno = $$step_ref[2];
          }
 
          $job_step[6] = $$step_ref[4]; # step name
          $job_step[7] = $$step_ref[5]; # RC
          $job_step[9] = $userid;
          push @jcl_job_step, \@job_step;
        }
      }
    }
 # }
  return \@jcl_job_step;
}
1;

sub scan_log_for_jobno_and_userid {   #scan all log to build the jobno_userid hash for later use
  my $logs_ref = shift;
  my %jobno_userid;
  foreach (@$logs_ref) {
    if (/(JOB\d{5}).*USERID (\w{6,7})  IS ASSIGNED TO THIS JOB/) {
        $jobno_userid{$1} = $2;
    } 
  }
  return \%jobno_userid;
}
1;

sub log_match {                     # looking for steps excution result for job_name in logs
  my ($job_name, $logs_ref) = @_;
  my @found_log;
  foreach (@$logs_ref) {
    if (/(\d{7}) (\S{11}) (JOB\d{5}).* -$job_name +(\S{5,8}) +(\S{2,5})/) {
      #date time jobno jobname stepname RC
      my $matched_string = "$1 $2 $3 $job_name $4 $5";
      my @matched_array = split / /, $matched_string;
      $matched_array[5] = '00' if $matched_array[5] eq 'NAME-';  # filter the RC = NAME-
#      push @found_log, $matched_string;
      push @found_log, \@matched_array;
#      print "$matched_string\n";
#      print "$_ " foreach @matched_array;
    }
  }
  return \@found_log;
}
1;

sub get_file_list {

  my $dir = shift;
  my @dir_files = <$dir*>;
  my @files;
  foreach (@dir_files) {
    if ( -f $_ ) {
      s/$dir//;
      push @files, $_;
    }
  }

  return \@files;
}
1;

sub get_today_file_list {
   my $dir = '/home/ftpuser/'; 
   @dir_files = </home/ftpuser/*>;
   my @files;
  foreach (@dir_files) {
    if ( -f $_ ) {
      s/$dir//;
      push @files, $_;
    }
  }
 
  return \@files;
}
1;

sub get_date_list {

  my $dir = shift;
  my @dir_files = <$dir*>;
  my @date;
#print "$_\n" foreach @dir_files;
  foreach (@dir_files) {
    if ( -d $_ && /^$dir\d{4}/) {
      s/$dir//;
      push @date, $_;
    }
  }
  @date = reverse sort @date;

  my $today;
  my $hhmm = &JCL::date_time('hhmm');
  if ($hhmm <= 830) {
    $today = &JCL::date_time('yesterday');
  }
  else {
    $today = &JCL::date_time('today');
  }
  my @last_six = ("$today", "$date[0]", "$date[1]", "$date[2]", "$date[3]", "$date[4]");
  return \@last_six;
}
1;

sub get_released_releasing {
  my ($dir, $file) = @_;
  $file = $dir . $file;
  my @jobs;
  my @jcls;
  my %released_by;
  my %releasing;
  my $i = 0;
  open (JCL, "< $file")
    || die "Cannot open $file: $!";
    while (<JCL>) {
#      @jcls = <JCL>;
      $jcls[$i] = $_;
      $i++;
    }
  close JCL;
  my $job = ' ';
  my $job_save = ' ';
  my $releasing_job = ' ';
#  print "$jcls[0]\n";
  foreach (@jcls) {
    my $after_match;
    if (/^\/\/([^*]{6,8})\s+JOB /) {
#    if (/^\/\/(.{8})\s+JOB/) {
      $after_match = $';
#      print "JOB=$1\n";
      if ($after_match !~ /[ ,]CLASS=[1A]/) {
        $job = $1;
#        print "JOB=$job\n";
      }
    }
#    if (/^\/\/PCHINRDR .*,PARM='([^*]{6,8})'/) {
    if (/^\/\/[^*].*PGM=LMN01.*,PARM='([^*]{6,8})'/) {
      $released_by{$1} = $job;  # released job
#      print "PARM=$1\n";
      if ($job eq $job_save) {
        $releasing_job = $releasing_job . " $1";
      }
      else {
        $releasing{$job_save} = $releasing_job;
        $releasing_job = $1;
        $job_save = $job;
      }
    }
  }
  $releasing{$job_save} = $releasing_job;
  return \%released_by, \%releasing;
}
1;

sub get_job_list {
  my ($dir, $file) = @_;
  $file = $dir . $file;
  my @jobs;
  open (JCL, "< $file")
    || die "Cannot create $file: $!";
  while (<JCL>) {
    my $after_match;
    if (/^\/\/([^*]{6,8})\s+JOB /) {
      $after_match = $';
      push @jobs, $1 if $after_match !~ /[ ,]CLASS=[1A]/;
    } 
  }
  close JCL;
  return \@jobs;
}
1;

sub date_from_yday {
  use Date::Calc qw/Add_Delta_Days/;
  my $year_yday = shift;
#  print "year_yday in sub=$year_yday\n";
  my $year = substr $year_yday, 0, 4;   # transfer day of year to real date
  my $yday = substr $year_yday, 4, 3;   # like 2010209 => 2010-07-28
  $yday = $yday - 1;
  my ($y,$month,$day) = Add_Delta_Days($year,1,1,$yday);
  $month = '0' . $month if $month < 10;
  $day = '0' . $day if $day < 10;
  return "$year-$month-$day";
}
1;

sub date_time {
#----<<--get date and time of now ------------------
  my $key_word = shift;
  use Time::Local;
  my $year  = (localtime)[5] + 1900;
  my $yday  = (localtime)[7] + 1;
  my $month = (localtime)[4] + 1;
  my $day   = (localtime)[3];
  my $hour  = (localtime)[2];
  my $min  = (localtime)[1];

  $month = "0" . $month if ( $month < 10 );
  $day = "0" . $day if ( $day < 10 );
  $hour = "0" . $hour if ( $hour < 10 );
  $min = "0" . $min if ( $min < 10 );
#  my $now = $year . $month . $day . $hour . $min;
  my $now = "$year/$month/$day $hour:$min";
  my $hhmm = $hour * 100 + $min;
  my $today = "$month" . "$day";
  return $hhmm if $key_word eq 'hhmm';
  return $today if $key_word eq 'today';
  return $now if $key_word eq 'now';
  return $yday if $key_word eq 'julian';
  if ($key_word eq 'yesterday') {
    my  @T=localtime(time-86400);
    $month = $T[4] + 1;
    $month = "0" . $month if ( $month < 10 );
    $day   = $T[3];
    $day = "0" . $day if ( $day < 10 );
    return $month . $day;
  }

}
1;
